/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        headerClass: 'text-center width-50',
        columnClass: 'text-center width-50',
        fastSort: true,
        fastQuery: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
        return "<a href='"+ncGlobal.adminRoot+"user/detail?userId="+value+"'>"+value+"</a>";
    }
    },
    {
        id: 'userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'ag.name',
        title: '所属代理商',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'a.available',
        title: '可用余额',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'realName',
        title: '真实姓名',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if(record.realName != undefined){
                return  "<xmp>"+record.realName+"</xmp>";
            }else{
                return  '';
            }
        }
    },
    {
        id: 'idCard',
        title: '身份证',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'alipayAccount',
        title: '支付宝账号',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'p.userName',
        title: '上级用户',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'type',
        title: '类型',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable:{
            1:$lang.userType.T1,
            2:$lang.userType.T2,
            3:$lang.userType.T3
        }
    },
    {
        id: 'lastLoginIp',
        title: '最后登录IP',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm',
    },
    {
        id: 'createTime',
        title: '注册时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'lastLoginDate',
        title: '最后登录时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    // {
    //     id: 'operation',
    //     title: '管理操作',
    //     type: 'string',
    //     'export':false,
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     extra: false,
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         // var view = "", audit = "";
    //         // var html;
    //         // if (record.realNameStatus == 2){
    //         //     audit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;审核&nbsp;</a>";
    //         // }else if (record.realNameStatus != 1){
    //         //     view = "<a data-target='#lookModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-eye Example of eye'></i>&nbsp;查看&nbsp;</a>";
    //         // }
    //         // html = view + audit;
    //         // return html;
    //
    //         return "<a href='"+ncGlobal.adminRoot+"user/detail?userId="+record.id+"' class='btn btn-sm btn-primary m-r-10'><i class='fa fa-eye Example of eye'></i>&nbsp;查看&nbsp;</a>";
    //     }
    // }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/user/json/list',
    exportURL: ncGlobal.adminRoot + 'user/json/export',
    exportFileName: '用户列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery|export[excel]',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","userType"],Timestamp:["createTime","birthday"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;

//排序
grid.sortParameter.columnId = ['desc_id','desc_createTime'];
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {
        // 编辑借款标对话框
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove();
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="realName"]').val(gridData.realName);
            modal.find('input[name="idCard"]').val(gridData.idCard);
            $('#editFormPicImg').attr('src', ncGlobal.fileRoot + gridData.idCardPicA);
            $('#editFormPicImg1').attr('src', ncGlobal.fileRoot + gridData.idCardPicB);
            modal.find('textarea[name="remark"]').val('');
            $(".status").bootstrapSwitch('state', true);
        });

        //查看对话框初始化
        $("#lookModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="realName"]').val(gridData.realName);
            modal.find('input[name="idCard"]').val(gridData.idCard);
            $('#lookFormPicImg').attr('src', ncGlobal.fileRoot + gridData.idCardPicA);
            $('#lookFormPicImg1').attr('src', ncGlobal.fileRoot + gridData.idCardPicB);
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_real_name_or_lk_user_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});